/**
 * Created by yangfan on 2016/8/31.
 */
define(['config/appregister',"../../services/httpServices","domReady","_","i18n"],function(app,httpService,domReady,_,i18n){
   app.filter("bracket",function(){
       return function (input) {
               return "("+input + ")"
       }
   });
    app.directive("refreshValue",function(){
            return {
                require:"ngModel",
                link:function(scope,element,attrs,ctrl){
                    scope.$watch("draw.size",function(newValue,oldValue){
                        if(newValue !=""){
                                ctrl.$setViewValue(scope.draw);
                        }
                    })
                }
            }
    }).directive("uiComment2",function(commentHttpServices){
            return {
                templateUrl:"app/business/home/directives/templates/uiComment.html",
                controller:function($scope,$element,$attrs,$q){
                    $scope.textarea = "";
                 //   var deferred = $q.defer();
                    this.support = function(item){
                        var type = 1;
                        var fileId = item.fileId;
                        var commentId = item.commentId;
                      var promise = commentHttpServices.addPraise(type,commentId,fileId);
                        return promise;
                    };
                    this.oppose = function(item){
                        var type = 2;
                        var commentId = item.commentId;
                      var promise =  commentHttpServices.addPraise(type,commentId);
                        return promise;
                    };
                    $scope.checkComment = function(item){
                          var req  = {
                              topId:item.commentId,
                              fileId:item.fileId,
                              uri:"getAllCommentsByTopId"
                          };
                      var promise = commentHttpServices.getComment(req);
                          promise.then(function(data){
                          $scope.secComment = data.list;
                      });
                        console.log( $scope.secComment);
                    };
                    $scope.publish = function(){
                       var promise =  commentHttpServices.publish($scope.textarea);
                        promise.then(function(data){
                            $scope.textarea = "";
                        })
                    };
                    //点击加载更多
                    var pageNo = 1;
                    $scope.loadMoreComment = function(){
                        pageNo++;
                      var promise =  commentHttpServices.getMoreComment(pageNo);
                        promise.then(function(response){
                            if(!response){return};
                          //  $scope.list.push(response.data.list);
                           var arr =  $scope.list.concat(response.data.list);
                            $scope.list = arr;
                        })
                    }
                },
                scope:true,
                link:function(scope,element,attrs){
                    scope.i18n = i18n;
                        var promise =  commentHttpServices.getMoreComment(1);
                        promise.then(function(response){
                            if(!response.data){return};
                            scope.list = response.data.list;
                            scope.num = response.data.Pagination.totalCount;
                        })
                }
                }
        }).directive("getPraise",function(){
        return {
            scope:{
                item:"=getItem",
                i18n:"=getI18n"
            },
            require:"^uiComment",
            template:'<a  ng-bind="i18n.support" ng-click="support(item)"></a><a href="" ng-bind="praiseCount |bracket"></a>' +
            '<a ng-bind="i18n.oppose" ng-click="oppose(item)"></a> <a href="" ng-bind="stampCount |bracket"></a>',
            link:function(scope,element,attrs,ctrl){
                scope.praiseCount = 0;
                scope.stampCount = 0;
              if(scope.item){
                  scope.praiseCount = scope.item.praiseCount;
                  scope.stampCount = scope.item.stampCount;
             };
                /*
                *根據後台返回情況 前台自動判斷點讚和踩得數量。
                * */
                scope.support = function(item){
                      var a =  ctrl.support(item)
                            .then(function(data){
                              scope.praiseCount+=1;
                            if(data.code&&data.code==110){
                                return;
                            }else if(data.code&&data.code==0){
                                scope.$apply(function(){
                                    scope.praiseCount+=1;
                                });
                            }
                        });
                    };
                scope.oppose = function(item){
                    var a =  ctrl.oppose(item)
                        .then(function(data){
                                scope.stampCount+=1;
                            if(data.code&&data.code==110){
                                return;
                            }else if(data.code&&data.code==0){
                                scope.$apply(function(){
                                    scope.stampCount+=1;
                                });
                            }
                        });
                }
                }
            }
    }).
        factory("commentHttpServices",function($q){

        if(localStorage.fileId){
            var fileId = localStorage.fileId;
        };
               return {
                   getComment:function(requstion){
                        var deferred = $q.defer();
                  var req ={
                type:"GET",
                uri:requstion.uri,
                data:{
                    fileId:requstion.fileId,
                    pageNo:1,
                    pageSize:10,
                    topId:requstion.topId
                }
            };
            httpService.getComment(req).success(function(response){
                if(!response.data)return;
                deferred.resolve(response.data);

            }).error(function(response){
                deferred.reject(response.data);
            });
                       return deferred.promise;
                   },
                   addPraise :function(type,commentId,fileId){
                     var deferred = $q.defer();
                     var  req = {
                type:"POST",
                uri:"addPraise",
                data:{
                    commentId:commentId,
                    type:type,
                    userId:1
                }
            };
            httpService.addPraise(req).success(function(response){
                if(!response)return;
                deferred.resolve(response);
            }).error(function(response){
                deferred.reject(response);
            });
             return deferred.promise;
        },
                   publish:function(textarea){
                       var deferred = $q.defer();
                       var  req = {
                           type:"POST",
                           uri:"add",
                           data:{
                               fileId:fileId,
                               userId:1,
                               content:textarea,
                               authStatus:1
                           }
                       };
                       httpService.addComment(req).success(function(response){
                           deferred.resolve(response);
                       }).error(function(){
                           deferred.reject(response);
                       })
                       return deferred.promise;
                   },
                   getMoreComment:function(pageNo){
                       var deferred = $q.defer();
                     var req ={
                     type:"GET",
                     uri:"getTopCommentsByFileId",
                     data:{
                        fileId:fileId,
                        pageNo:pageNo,
                        pageSize:5,
                        all:false
                    }
                };
                httpService.getComment(req).success(function(response){
                    if(!response.data)return;
                    deferred.resolve(response);
                    //$scope.$apply(function(){
                    //    $scope.comment = response.data.list;
                    //    $scope.num = $scope.comment.length;
                    //});
                }).error(function(){
                    deferred.reject(response);
                });
                       return deferred.promise;
            }
               }
    })
});
